﻿using Company.Database.Base.Entities;
using Company.Database.Base.Repositories;
using Company.WebServer.Models;
using Microsoft.AspNetCore.Mvc;

namespace Company.WebServer.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class UserController : ControllerBase
    {
        private readonly ILogger<WeatherForecastController> _logger;
        private IUserRepository UserRepository { get; }

        public UserController(
            ILogger<WeatherForecastController> logger,
            IUserRepository userRepository)
        {
            UserRepository = userRepository;
            _logger = logger;
        }
        [HttpGet]
        [Route("LoginByGet")]
        public async Task<IActionResult> Login([FromQuery] string username, string password,int hospitalId)
        {
            Result<UserEntity> result = new();
            var entities = await UserRepository.GetAllAsync();
            if(entities==null|| !entities.Any())
            {
                return new JsonResult(result);
            }
            var entity = entities.FirstOrDefault(t => t.Name == username && t.Password == password&&t.HospitalId==hospitalId);
            if (entity != null)
            {
                result.Success = true;
                result.Message = "恭喜你登录成功";
                result.Value = entity;
            }
            return new JsonResult(result);
        }
    }
}
